/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.managers.actionlisteners;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.toolkit.Toolkit;

/**
 *
 * @author Mido
 */
public class ApplyLocalizationAction implements ActionListener {

    private String locale;  // the selected language to be applied

    public ApplyLocalizationAction(String locale) {
        this.locale = locale;
    }

    /**
     * The action that will be performed by this action listener
     *
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        // change localization
        LocalizationManager.saveCurrentResource(locale);

        // inform the user about the change
        Object[] options = {LocalizationManager.getCurrent_resource().getString("RESTART_NOW"),
            LocalizationManager.getCurrent_resource().getString("DONT_OPEN_FILE")};

        // ask the user if he/she wants to restart the program
        // in order to apply the language settings correctly
        if (JOptionPane.showOptionDialog(null, LocalizationManager.getCurrent_resource().getString("LANG_CHANGE_TEXT"),
                LocalizationManager.getCurrent_resource().getString("LANG_CHANGE_TITLE"), JOptionPane.YES_NO_OPTION, 1, null, options, options[0]) == 0) {
            try {
                // open a new instance of the program
                Desktop.getDesktop().open(new File("JPDFTools.jar"));
                // terminate the current instance of the program
                LoggerManager.getLogger().log(Level.INFO, "Program is restarting...{0}", Toolkit.getLineSeparator());
                System.exit(0);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_LANGUAGE_SETTINGS_ERROR"),
                        LocalizationManager.getCurrent_resource().getString("ERROR"), 0);
                LoggerManager.getLogger().log(Level.SEVERE, "An excption occurred. {0}", Toolkit.getStackTraceAsString(ex.getCause()));
            }
        }
    }
}
